using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace LFramework
{
    /// <summary>
    /// 加密
    /// </summary>
    public sealed class EncryptUtil
    {
        public static string Md5(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytResult = md5.ComputeHash(Encoding.Default.GetBytes(value));
            string strResult = BitConverter.ToString(bytResult);
            strResult = strResult.Replace("-", "");
            return strResult;
        }

        /// <summary>
        /// 获取文件的MD5
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetFileMD5(string filePath)
        {
            if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
            {
                return null;
            }

            try
            {
                FileStream file = new FileStream(filePath, FileMode.Open);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bytResult = md5.ComputeHash(file);
                string strResult = BitConverter.ToString(bytResult);
                strResult = strResult.Replace("-", "");
                return strResult;
            }
            catch
            {
                return null;
            }
        }
    }
}